﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using Baimasoft.FrameWork.Base;
using Baimasoft.Manage.SqlServerDAL;
using HsTec.Mobile.Green.SqlServerDAL;

public partial class DocumentManage_Document_Add_Left : System.Web.UI.Page
{
    LogManage MyLog = new LogManage();
    DataFile MyDF = new DataFile();
    DataFolder datafolder = new DataFolder();

    BaiMaSoft.Conn.DataConn MyDataConn = new BaiMaSoft.Conn.DataConn();
    BaimasoftFile MyFile = new BaimasoftFile();
    public SqlConnection myconn;
    public SqlCommand mycomm, mycomm3;
    public SqlDataReader mydr;
    public SqlDataAdapter myadapter;
    public DataSet myds;
    public DataView mydv;
    public DataTable mytb;
    public DataRow myrow;
    public DataColumn mycolumn;
    public string filedtotal = "";
    public string sHtml = "";
    public string sOrgCode = "";
    public string sDeptHtml = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string sChoiceFolderCol = "0";
            if ((string)Session["Baimasoft_User_LogonID"] != "admin")
            {
                sChoiceFolderCol  = AspNetHelper.GetRightFolderIDCol();
                Session["SChoiceFolderCol"] = sChoiceFolderCol.ToString();
            }

            HRootPath.Value = ConfigurationManager.AppSettings["DocumentMange"];  //取Web.Config文件中的配置信息,在配置的时候要注意该盘有定怕权限
            string sID = "0";
            IniInfo(sID, sChoiceFolderCol);
        }
    }
    public void IniInfo(string HID, string sPageIDCol)
    {
        MyDataConn.Connection.Open();
        string sSql = @"  SELECT ID, PaterID as ParentID,ChildNumber, Name,replace(Path,'\','\\\\') as Path,Name,replace(PaterPath,'\','\\\\') as  PaterPath FROM DataFolder order by DOrder ";

        myadapter = new SqlDataAdapter(sSql, MyDataConn.Connection);
        myds = new DataSet();
        myadapter.Fill(myds, "T_DocumentManage");
        mytb = myds.Tables["T_DocumentManage"];

        foreach (DataColumn mycolumn in mytb.Columns)
        {
            filedtotal = filedtotal + mycolumn.ColumnName + ",";
        }

        sDeptHtml = sDeptHtml + "<script language=\"javascript\" type=\"text/javascript\">";
        filedtotal = filedtotal.Substring(0, filedtotal.Length - 1);
        sDeptHtml = sDeptHtml + " function tree(" + filedtotal + "){";
        string[] sfiledtotal = filedtotal.Split(',');
        int nfiledtotal = sfiledtotal.Length;
        for (int i = 0; i < nfiledtotal; i++)
        {
            sDeptHtml = sDeptHtml + "this." + sfiledtotal[i] + "=" + sfiledtotal[i] + ";";
        }

        if (mytb.Rows.Count > 0)
        {
            int iRowCount = mytb.Rows.Count - 1;
            sDeptHtml = sDeptHtml + "}var trees =new Array(" + iRowCount + ");";
        }
        else
        {
            sDeptHtml = sDeptHtml + "}var trees =new Array(0);";
        }

        for (int j = 0; j < mytb.Rows.Count; j++)
        {
            sDeptHtml = sDeptHtml + "trees[" + j + "] = new tree(";
            string temp = "";
            for (int k = 0; k < mytb.Columns.Count; k++)
            {
                temp = temp + "'" + mytb.Rows[j][k] + "'";
                if (k != mytb.Columns.Count - 1)
                {
                    temp = temp + ",";
                }
            }
            sDeptHtml = sDeptHtml + temp + ");";
        }
        sDeptHtml = sDeptHtml + "GetSuperOrgID('" + HID + "');showtree(0,'" + sPageIDCol + "');SpreadNewOrgTree();";
        sDeptHtml = sDeptHtml + "</script>";
        sDeptHtml = sDeptHtml + "";
        MyDataConn.Connection.Close();
    }

    /// <summary>
    /// 刷新按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void BtnRefresh_Click(object sender, EventArgs e)
    {
        string sBack = HID.Value.ToString().Trim();
        if ((string)Session["SChoiceFolderCol"] != null)
        {
            IniInfo(sBack, (string)Session["SChoiceFolderCol"]);
        }
        ClearItem();
    }


    //清空
    public void ClearItem()
    {
        HID.Value = "";
    }


    /// <summary>
    /// 删除文件夹
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void BtnCut_Click(object sender, EventArgs e)
    {
        try
        {
            string sID = HID.Value.ToString().Trim(); //当前结点ID
            string sPaterID = HPaterID.Value.ToString().Trim(); //上级结点ID
            int iFilenum = 0;

            iFilenum = MyDF.GetCountByqueryStr (" FolderID='" + sID + "'");
            if (iFilenum <= 0)
            {
                datafolder .DeleteDataFolderInfo (Convert.ToInt32(sID));

                datafolder.UPChildNumber(sPaterID);  //删除的同时要更新上级子目录数
                string m_Root = ConfigurationManager.AppSettings["DocumentMange"];  //取Web.Config文件中的配置信息
                string str = m_Root + HPath.Value.ToString().Trim();              //写到服务器上的路径

                MyFile.DeleteDirectory(str);  //删除目录

                //日志
                try
                {
                    string sPerID = (string)Session["Baimasoft_Pers_ID"];
                    string sContent = "用户" + (string)Session["Baimasoft_User_LogonID"] + "删除文件夹：" + str;
                    string sIP = Page.Request.UserHostAddress;
                    MyLog.InsertLog("3", sPerID, sContent, sIP);
                }
                catch
                {
                    string sContent = "删除文件夹：" + str;
                    string sIP = Page.Request.UserHostAddress;
                    MyLog.InsertLog("3", "0", sContent, sIP);
                }

                if ((string)Session["SChoiceFolderCol"] != null)
                {
                    IniInfo(sPaterID, (string)Session["SChoiceFolderCol"]);
                }

                ClearItem();
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请先移去或删除该文件夹中的文件,再删除文件夹!');</script>");


                if ((string)Session["SChoiceFolderCol"] != null)
                {
                    IniInfo(sPaterID, (string)Session["SChoiceFolderCol"]);
                }
                
                ClearItem();
                return;
            }
        }
        catch
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('删除文件夹失败!');</script>");
            return;
        }
    }
}
